const promisify = require("./promisify");
const extendWithJsSources = require("./src");
const path = require("path");
const { resolvePath } = require("./commons");

/*const isElectronWebpack =
  // assume module required by webpack if no system path inv envs
  !process.env.path
  // detect if electron https://github.com/electron/electron/issues/2288
  && global.window && global.window.process && global.window.process.type
  && global.navigator && ((global.navigator.userAgent || '').toLowerCase().indexOf(' electron/') > -1)

let cv = isElectronWebpack ? require('./Release/opencv4nodejs.node') : require('./cv')*/

let cv = null;
try {
  cv = require("./Release/opencv4nodejs.node");
  const { haarCascades, lbpCascades } = cv;
  Object.keys(haarCascades).forEach((key) => {
    cv[key] = resolvePath(
      path.join(__dirname, "./haarcascades"),
      haarCascades[key]
    );
  });
  Object.keys(lbpCascades).forEach((key) => {
    cv[key] = resolvePath(
      path.join(__dirname, "./lbpcascades"),
      lbpCascades[key]
    );
  });
} catch (err) {
  console.log(err);
  cv = {};
}

// promisify async methods
cv = promisify(cv);
cv = extendWithJsSources(cv);

module.exports = cv;
